package org.summer.lawyer.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.summer.common.core.domain.R;
import org.summer.common.core.validate.AddGroup;
import org.summer.common.core.validate.EditGroup;
import org.summer.common.excel.utils.ExcelUtil;
import org.summer.common.idempotent.annotation.RepeatSubmit;
import org.summer.common.log.annotation.Log;
import org.summer.common.log.enums.BusinessType;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.common.web.core.BaseController;
import org.summer.lawyer.domain.bo.brokerageuser.BrokerageUserBo;
import org.summer.lawyer.domain.bo.brokerageuser.CommissionBo;
import org.summer.lawyer.domain.vo.brokerageuser.BrokerageUserVo;
import org.summer.lawyer.service.IBrokerageRecordService;
import org.summer.lawyer.service.IBrokerageUserService;

import java.util.List;

/**
 * 分销员
 *
 * @author summer
 * @date 2024-06-20
 */
@Tag(name = "后台相关接口", description = "后台分销员相关接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lawyer/brokerageUser")
public class BrokerageUserController extends BaseController {

    private final IBrokerageUserService brokerageUserService;
    private final IBrokerageRecordService brokerageRecordService;
    /**
     * 查询分销员列表
     */
    @Operation(summary = "查询分销员列表", description = "查询分销员列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("brokerage:user:list")
    @GetMapping("/list")
    public TableDataInfo<BrokerageUserVo> list(BrokerageUserBo bo, PageQuery pageQuery) {
        return brokerageUserService.selectPageList(bo, pageQuery);
    }

    /**
     * 导出分销员列表
     */
    @Operation(summary = "导出分销员列表", description = "导出分销员列表")
    @SaCheckPermission("brokerage:user:export")
    @Log(title = "分销员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BrokerageUserBo bo, HttpServletResponse response) {
        List<BrokerageUserVo> list = brokerageUserService.selectBrokerageUserVoList(bo);
        ExcelUtil.exportExcel(list, "分销员", BrokerageUserVo.class, response);
    }

    /**
     * 获取分销员详细信息
     *
     * @param brokerageUserId 主键
     */
    @Operation(summary = "查询分销员列表", description = "查询分销员列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("brokerage:user:query")
    @GetMapping("/{brokerageUserId}")
    public R<BrokerageUserVo> getInfo(@NotNull(message = "主键不能为空")
                                      @PathVariable Long brokerageUserId) {
        return R.ok(brokerageUserService.selectBrokerageUserVoById(brokerageUserId));
    }

    /**
     * 新增分销员
     */
    @Operation(summary = "新增分销员", description = "新增分销员", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("brokerage:user:add")
    @Log(title = "分销员", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/saveBrokerageUser")
    public R<Void> saveBrokerageUser(@Validated(AddGroup.class) @RequestBody BrokerageUserBo bo) {
        return toAjax(brokerageUserService.insertByBo(bo));
    }

    /**
     * 修改分销员
     */
    @Operation(summary = "修改分销员", description = "修改分销员", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("brokerage:user:edit")
    @Log(title = "分销员", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/updateBrokerageUser")
    public R<Void> updateBrokerageUser(@Validated(EditGroup.class) @RequestBody BrokerageUserBo bo) {
        return toAjax(brokerageUserService.updateByBo(bo));
    }

    /**
     * 删除分销员
     *
     * @param brokerageUserIds 主键串
     */
    @Operation(summary = "删除分销员", description = "删除分销员", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("brokerage:user:remove")
    @Log(title = "分销员", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeBrokerageUser/{brokerageUserIds}")
    public R<Void> removeBrokerageUser(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] brokerageUserIds) {
        return toAjax(brokerageUserService.deleteWithValidByIds(List.of(brokerageUserIds), true));
    }

    @Operation(summary = "启用推广员", description = "启用推广员", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("brokerage:user:active")
    @Log(title = "启用推广员", businessType = BusinessType.UPDATE)
    @PostMapping("/activeBrokerageUser")
    public R<Void> activeBrokerageUser(@Validated @RequestBody BrokerageUserBo bo){
        bo.setStatus("1");
        brokerageUserService.changeBrokerageUserStatus(bo);
        return R.ok();
    }

    @Operation(summary = "禁用推广员", description = "禁用推广员", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("brokerage:user:inactive")
    @Log(title = "禁用推广员", businessType = BusinessType.UPDATE)
    @PostMapping("/inactiveBrokerageUser")
    public R<Void> inactiveBrokerageUser(@Validated @RequestBody BrokerageUserBo bo){
        bo.setStatus("0");
        brokerageUserService.changeBrokerageUserStatus(bo);
        return R.ok();
    }

    @SaCheckPermission("brokerage:user:commission")
    @Log(title = "结算佣金", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PostMapping("/commissionSettlement")
    public R<Void> commissionSettlement(@Validated @RequestBody CommissionBo bo){
        brokerageUserService.commissionSettlement(bo);
        return R.ok();
    }
}
